import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_user.dart';
import 'package:flutter_learn_vi/src/common/user_helper.dart';
import 'package:flutter_learn_vi/src/model/user_model.dart';
import 'package:flutter_learn_vi/src/utils/date_utils.dart';
import 'package:flutter_learn_vi/src/utils/navigator_utils.dart';
import 'package:flutter_learn_vi/src/utils/string_utils.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';
import 'package:url_launcher/url_launcher.dart';

import 'mine_person_resum_page.dart';
import 'mine_work_recoder_page.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2020/12/11.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class MineAddresPersionInfoPage extends StatefulWidget {
  final UserBean userBean;

  const MineAddresPersionInfoPage({Key key, this.userBean}) : super(key: key);

  @override
  _MineAddresPersionInfoPageState createState() =>
      _MineAddresPersionInfoPageState();
}

class _MineAddresPersionInfoPageState extends State<MineAddresPersionInfoPage>
    with UserModel {
  UserBean _userBean;

  @override
  void initState() {
    super.initState();
    _userBean = widget.userBean;
    refreshUserInformation();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("个人信息"),
        centerTitle: true,
        elevation: 1.0,
      ),

      ///填充布局
      body: Container(
          width: double.infinity,
          height: double.infinity,
          child: SingleChildScrollView(
            child: Column(
              children: [
                Container(
                  padding: EdgeInsets.all(8),
                  margin: EdgeInsets.only(bottom: 10),
                  color: Colors.white,
                  child: Column(
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      SizedBox(
                        height: 8,
                      ),
                      Row(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          buildUserImage(),
                          SizedBox(
                            width: 12,
                          ),
                          Expanded(
                            child: Column(
                              crossAxisAlignment: CrossAxisAlignment.start,
                              mainAxisSize: MainAxisSize.min,
                              children: [
                                Text(
                                  "${_userBean.reamName}",
                                  style: TextStyle(
                                    fontWeight: FontWeight.w500,
                                    fontSize: 18,
                                    color: Colors.blueGrey[700],
                                  ),
                                ),
                                SizedBox(
                                  height: 4,
                                ),
                                Text(
                                  "${_userBean.department}-${_userBean.userPost}",
                                  style: TextStyle(
                                    fontSize: 12,
                                    color: Colors.black54,
                                  ),
                                ),
                                // Text(
                                //   "在科兴工作了890天",
                                //   style: TextStyle(
                                //     fontSize: 12,
                                //     color: Colors.black54,
                                //   ),
                                // ),
                              ],
                            ),
                          ),
                        ],
                      ),
                      Divider(
                        height: 20,
                      ),
                      Row(
                        children: [
                          TextButton(
                            onPressed: () {
                              NavigatorUtils.pushPage(
                                context,
                                MineWorkRecoderPage(
                                  userBean: widget.userBean,
                                ),
                              );
                            },
                            child: Text("查看人事档案"),
                          ),
                          // SizedBox(
                          //   width: 22,
                          // ),
                          // TextButton(
                          //   onPressed: () {
                          //     // NavigatorUtils.pushPage(
                          //     //     context, MineLineResumPage());
                          //     // NavigatorUtils.pushPage(
                          //     //   context,
                          //     //   MineLineResumPage(
                          //     //     userBean: widget.userBean,
                          //     //   ),
                          //     // );
                          //     NavigatorUtils.pushPage(
                          //       context,
                          //       PdfWidgetPage(
                          //         title: "在线简历",
                          //         url: HttpHelper.userWorkLine + "/${widget.userBean.userId}",
                          //         isPdf: false,
                          //       ),
                          //
                          //     );
                          //
                          //   },
                          //   child: Text("查看在线简历"),
                          // ),
                          SizedBox(
                            width: 22,
                          ),
                          TextButton(
                            onPressed: () {
                              NavigatorUtils.pushPage(
                                  context,
                                  MinePersonResumPage(
                                    userBean: widget.userBean,
                                  ));
                            },
                            child: Text("工作简历"),
                          ),
                        ],
                      )
                    ],
                  ),
                ),
                buildOther(),
              ],
            ),
          )),
    );
  }

  Container buildContainer(String leftText, String rightText) {
    if (rightText == null) {
      rightText = "--";
    }
    return Container(
      height: 33,
      child: Row(
        children: [Expanded(child: Text("$leftText")), Text("$rightText")],
      ),
    );
  }

  buildOther() {
    if (_userBean.department == "中台学习") {
      return Container(
        padding: EdgeInsets.all(8),
        color: Colors.white,
        margin: EdgeInsets.only(bottom: 10),
        child: buildContainer("账号", _userBean.mobile),
      );
    }
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Container(
          child: Row(
            children: [
              Expanded(
                child: buildContainer("手机号", _userBean.mobile),
              ),
              SizedBox(
                width: 12,
              ),
              InkWell(
                onTap: () {
                  launchTelURL(_userBean.mobile);
                },
                child: Container(
                  width: 44,
                  padding:
                  EdgeInsets.only(left: 10, right: 4, bottom: 10, top: 10),
                  child: Image.asset(
                    "assets/images/2.0x/phone_icon.png",
                  ),
                ),
              ),
            ],
          ),
          padding: EdgeInsets.all(8),
          color: Colors.white,
          margin: EdgeInsets.only(bottom: 10),
        ),
        Container(
          padding: EdgeInsets.all(8),
          color: Colors.white,
          margin: EdgeInsets.only(bottom: 10),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              buildContainer("账号", _userBean.username),
              Divider(),
              buildContainer("员工号", _userBean.staffNum),
              Divider(),
              buildContainer("邮箱", _userBean.userMailbox),
            ],
          ),
        ),
        Container(
          padding: EdgeInsets.all(8),
          color: Colors.white,
          margin: EdgeInsets.only(bottom: 10),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              buildContainer("民族", _userBean.userNation),
              Divider(),
              buildContainer("身份证号", _userBean.idCard),
              Divider(),
              buildContainer("学历", _userBean.educationalBackground),
              Divider(),
              buildContainer("学院", _userBean.userAcademy),
              Divider(),
              buildContainer("学院性质", _userBean.userSchoolNature),
              Divider(),
              buildContainer("专业", _userBean.userMajor),
              Divider(),
              buildContainer("职称", _userBean.technicalTitle),
              Divider(),
              buildContainer("政治面貌", _userBean.politicsStatus),
              Divider(),
              buildContainer(
                  "入党时间", ViDateUtils.formatDateStr(_userBean.factionTime)),
            ],
          ),
        ),
        Container(
          padding: EdgeInsets.all(8),
          color: Colors.white,
          margin: EdgeInsets.only(bottom: 10),
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              buildContainer(
                  "参加工作时间", ViDateUtils.formatDateStr(_userBean.entryTime)),
              Divider(),
              buildContainer("籍贯", _userBean.userAddress),
              Divider(),
              buildContainer(
                  "生日", ViDateUtils.formatDateStr(_userBean.birthday)),
              Divider(),
              buildContainer("个性签名", _userBean.autograph),
            ],
          ),
        )
      ],
    );
  }

  /// 调起拨号页
  static void launchTelURL(String phone) async {
    String url = 'tel:' + phone;
    if (await canLaunch(url)) {
      await launch(url);
    } else {
      ToastUtils.showToast('拨号失败！');
    }
  }

  buildUserImage() {
    return ClipRRect(
      borderRadius: BorderRadius.all(Radius.circular(4)),
      child: Container(
        color: Colors.blueGrey[200],
        child: StringUtils.isEmpty(_userBean.identifImg)
            ? Image.asset(
                "assets/images/2.0x/app_icon_yuxi.png",
                height: 66,
                width: 66,
              )
            : CachedNetworkImage(
                imageUrl: _userBean.identifImg,
                height: 66,
                width: 48,
              ),
      ),
    );
  }

  //刷新用户信息
  void refreshUserInformation() {
    int userId = widget.userBean.userId;
    int selefUserId = UserHelper.getInstance.userBean.userId;
    if (userId == selefUserId) {
      getUserInfo();
    }
  }
}
